// 这份代码专门处理逻辑层的操作
const focuscardDao = require('../dao/focuscardDao');
const powerDao = require('../dao/powerDao');

const focuscardService = {
    /*
     * 专注打卡接口
     */
    async focuscard(uid, time, duration) {
        let result;
        try {
            result = await focuscardDao.focuscard(uid, time, duration);
            result = { success: true, msg: '专注模式打卡成功' };
        } catch (err) {
            result = { success: false, msg: err };
        }
        return result;
    },
    /*
     * 打卡记录统计接口
     * 返回：总专注时长、日均专注时长、连续打卡几天
     */
    async checkinStatistics(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看专注排行榜');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await focuscardDao.checkinStatistics(uid);
            if (data.length) {
                data = data[0];
                data.avg_duration = data.all_duration / data.total_days;
                result = { success: true, msg: '获取专注模式统计数据成功', data };
            } else {
                result = { success: true, msg: '获取专注模式统计数据成功', data: {
                        all_duration: 0,
                        avg_duration: 0,
                        total_days: 0,
                        consecutiveDays: 0
                    }
                };
            }
        } catch (err) {
            result = { success: false, msg: '数据库异常' };
        }
        return result;
    },
    /*
     * 排行榜接口
     * 返回：前100的名次
     */
    async checkinRanking(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看专注排行榜');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await focuscardDao.checkinRanking(uid);
            result = { success: true, msg: '获取专注模式排行榜成功', data };
        } catch (err) {
            result = { success: false, msg: '数据库异常' };
        }
        return result;
    },
    async checkinTrend(uid) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '查看专注排行榜');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await focuscardDao.checkinTrend(uid);
            result = { success: true, msg: '获取一周打卡记录趋势成功', data };
        } catch (err) {
            result = { success: false, msg: '数据库异常' };
        }
        return result;
    },
    async getAllFocuscard(uid, page) {
        let result;
        // 鉴权
        try {
            let power = await powerDao.checkPower(uid, '打卡管理');
            if (power.length == 0) {
                result = { success: false, msg: '权限不足' };
                return result;
            }
        } catch (err) {
            result = { success: false, msg: "数据库异常" };
            return result;
        }
        // 业务逻辑
        try {
            let data = await focuscardDao.getAllFocuscard(uid, page);
            result = { success: true, msg: '获取所有专注模式打卡记录成功', data };
        } catch (err) {
            result = { success: false, msg: '数据库异常' };
        }
        return result;
    }
};

module.exports = focuscardService;